--[[
   file:download_http_header_filter.lua
   author:yuan jie
   date:2015-07-16.lua
   description:This lua file is service as download filename url encode in http response header
   vesion: v1.0
   modified history:
--]]

function url_decode(s)
    s = string.gsub(s, '%%(%x%x)', function(h) return string.char(tonumber(h, 16)) end)
    return s
end

function url_encode(s)
    s = string.gsub(s, "([^%w%.%- ])", function(c) return string.format("%%%02X", string.byte(c)) end)
    return string.gsub(s, " ", "+")
end

if ngx.var.is_rename == "yes" then

  local file_real_name = url_decode(ngx.var.to_urlencode_filename);

	if ngx.var.need_urlencode == "yes" then
      file_real_name = url_encode(file_real_name);
		  ngx.header["debug"]='need urlencode';
  else
      ngx.header["debug"]='don not need urlencode';
	end

	ngx.header["Content-Disposition"]='attachment; filename="'..file_real_name..'"';

end

ngx.header["production"]='wowostar unclone';

